package com.life.wd.service.Imp;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.life.wd.dto.UserDTO;
import com.life.wd.mapper.UserMapper;
import com.life.wd.result.PageResult;
import com.life.wd.service.UserService;
import com.life.wd.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImp implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageResult pageQuery(UserDTO userDTO) {
        PageHelper.startPage(userDTO.getPage(), userDTO.getPageSize());
        Page<UserVO> users = userMapper.pageQuery(userDTO);
        getUserFansAndFollow(users.getResult());
        return new PageResult(users.getTotal(), users.getResult());
    }

    public void getUserFansAndFollow(List<UserVO> list) {
        for (UserVO userVO : list) {
            Integer id = userVO.getId();
            Integer fansNum = userMapper.selectFans(id);
            Integer followNum = userMapper.selectFollow(id);
            Integer videoNum = userMapper.selectVideoNum(id);
            userVO.setVideoNum(videoNum);
            userVO.setFans(fansNum);
            userVO.setFollow(followNum);
        }
    }
}
